from flask import Blueprint, request
from flask_restful import Api, Resource, reqparse

from apps.utils import login_required
from apps.neo4j.InterfaceSet.OutProvided import GetProblemData_By_ProduceCode, tree1_data, fuzzy_query, \
    all_Relation_Json, exactSearch, fuzzy_unique_query, details
from apps.neo4j.InterfaceSet.OutProvided import all_data, add_node_relation
from apps.models.search_record import Search
from exts import db
import json
import logging

test_bp = Blueprint('test', __name__)
api = Api(test_bp)


test_parser = reqparse.RequestParser()
test_parser.add_argument('id', type=str, help='必须填写设备型号', required=True)


class TestAPI(Resource):
    def get(self):
        args = test_parser.parse_args()
        id1 = args.get('id')
        search = Search()
        search.search_word = id1
        db.session.add(search)
        db.session.commit()
        # print(GetProblemData_By_ProduceCode(id1)[0])
        return GetProblemData_By_ProduceCode(id1)

    @login_required
    def post(self):
        return {"status": 200}


tree_parser = reqparse.RequestParser()
tree_parser.add_argument('id', type=str, help='必须填写部件', required=True)


class TreeAPI(Resource):
    def get(self):
        args = tree_parser.parse_args()
        value = args.get('id')
        res = tree1_data(value)
        return res


search_parser = reqparse.RequestParser()
search_parser.add_argument('id', type=str, help='必须填写故障描述', required=True)


class SearchAPI(Resource):
    def get(self):
        args = search_parser.parse_args()
        keywords = args.get('id')
        res = fuzzy_query(keywords)
        # print(res)
        return res


add_parser = reqparse.RequestParser()
add_parser.add_argument("part", location='form')
add_parser.add_argument('phenomenon', location='form')
add_parser.add_argument('location', location='form')
add_parser.add_argument('cause', location='form')
add_parser.add_argument('material', location='form')
add_parser.add_argument('method', location='form')


class AddAPI(Resource):
    def post(self):
        args = add_parser.parse_args()
        part = args.get('part')
        phenomenon = args.get('phenomenon')
        location = args.get('location')
        cause = args.get('cause')
        material = args.get('material')
        method = args.get('method')
        data = {
            "part": part,
            "phenomenon": phenomenon,
            "location": location,
            "cause": cause,
            "material": material,
            "method": method,
        }
        # 调用函数
        label = add_node_relation(data)

        # print(part, phenomenon, method)
        if label == 0:
            return {"status": 200}
        else:
            return {"status": 500}


class AllDataAPI(Resource):
    def get(self):
        res = all_data()
        return res

class GraphAPI(Resource):
    def get(self):
        res = all_Relation_Json()
        return res


esearch_parser = reqparse.RequestParser()
esearch_parser.add_argument('id', type=str, help='必须填写现象', required=True)
class ExactSearchAPI(Resource):
    def get(self):
        args = esearch_parser.parse_args()
        keywords = args.get('id')
        res = exactSearch(keywords)
        return res


search_unique_parser = reqparse.RequestParser()
search_unique_parser.add_argument('msg', type=str, required=True)
class SearchUniqueAPI(Resource):
    def get(self):
        args = search_unique_parser.parse_args()
        keywords = args.get('msg')
        print(keywords)
        res = fuzzy_unique_query(keywords)
        return res

details_parser = reqparse.RequestParser()
details_parser.add_argument('pid', type=str, required=True)
details_parser.add_argument('nid', type=str, required=True)

class DetailsAPI(Resource):
    def get(self):
        args = details_parser.parse_args()
        pid = args.get('pid')
        nid = args.get('nid')
        res = details(pid, nid)
        print(res)
        return res

api.add_resource(TestAPI, '/test')
api.add_resource(TreeAPI, '/tree')
api.add_resource(SearchAPI, '/fsearch')
api.add_resource(AddAPI, '/add_part')
api.add_resource(AllDataAPI, '/add_back')
api.add_resource(GraphAPI, '/graph')
api.add_resource(ExactSearchAPI, '/esearch')
api.add_resource(SearchUniqueAPI, '/fsearch_unique')
api.add_resource(DetailsAPI, '/details')